package {
	import flash.display.Sprite;
	import flash.events.Event;
	
	//[SWF(width="500", height="500", backgroundColor="#f0f0f0", frameRate=60)]

	public class ShapeBench extends Sprite
	{
		private var _stats_ex:StatsEx;
		private var _fcnt:uint;
		private var _ary_shape:Array;
		
		public static const SHAPE_RADIUS:int = 40;
		
		private const SHAPE_NUM:int = 100;
		private const STAGE_WIDTH:int = 500;
		private const STAGE_HEIGHT:int = 500;
		
		public function ShapeBench()
		{
			var tgt_shape:ShapePerticle;
			var r:int ,g:int, b:int;
			
			_ary_shape = [];
			
			for( var i:int = 0, len:int = SHAPE_NUM; i < len; i++)
			{
				r = g = b = 0;
				
				switch( i % 7 )
				{
					case 0:
					{
						r = i;
					}
					break;
					
					case 1:
					{
						g = i;
					}
					break;
					
					case 2:
					{
						b = i;
					}
					break;
					
					case 3:
					{
						r = g = i;
					}
					break;
					
					case 4:
					{
						r = b = i;
					}
					break;
					
					case 5:
					{
						g = b = i;
					}
					break;
					
					case 6:
					{
						r = g = b = i;
					}
					break;
				}
				
				r *= 2;
				g *= 2;
				b *= 2;
				
				tgt_shape = new ShapePerticle(r, g, b);
				
				_ary_shape[i] = tgt_shape;
				
				tgt_shape.x = i * 4;
				tgt_shape.y = 200;
				
				tgt_shape.dir_x = i & 1;
				tgt_shape.dir_y = i & 1;
				
				addChild(tgt_shape);
			}
			
			
			_stats_ex = new StatsEx();
			addChild( _stats_ex );
			
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		
		private function onEnterFrameHandler(event:Event):void
		{
			var tgt_shape:ShapePerticle;
			
			++_fcnt;
			
			if( _fcnt == 240 )
				_stats_ex.calculateFPSAverageStart();
			else if( _fcnt == 800 )
				_stats_ex.calculateFPSAverageEnd();
				
			for( var i:int = 0, len:int = SHAPE_NUM; i < len; i++)
			{
				tgt_shape = _ary_shape[i];
				
				
				if( tgt_shape.dir_x )
				{
					++tgt_shape.x;
					
					if( (tgt_shape.x + SHAPE_RADIUS) > STAGE_WIDTH )
					{
						tgt_shape.dir_x = 0;
						tgt_shape.x -= 2;
					}
				}
				else if( --tgt_shape.x - SHAPE_RADIUS < 0 )
				{
					tgt_shape.dir_x = 1;
					tgt_shape.x += 2;
				}
				
/* 				if( tgt_shape.dir_y )
				{
					++tgt_shape.y;
					
					if( (tgt_shape.y + SHAPE_RADIUS * 2) > STAGE_HEIGHT )
					{
						tgt_shape.dir_y = 0;
						tgt_shape.y -= 2;
					}
				}
				else if( --tgt_shape.y < 0 )
				{
					tgt_shape.dir_y = 1;
					tgt_shape.y += 2;
				} */

			}

		}

	}
}

import flash.display.Shape;

class ShapePerticle extends Shape
{
	private var _col:Number;
	
	public var dir_x:int; 
	public var dir_y:int; 
	
	public function ShapePerticle(r:uint = 255, g:uint = 255, b:uint = 255)
	{
		r &= 0xff;
		g &= 0xff;
		b &= 0xff;

		_col = (r << 16) + ( g << 8) + b; 
		
		graphics.beginFill(_col, 0.5);
		graphics.drawCircle(0, 0, ShapeBench.SHAPE_RADIUS);
		graphics.endFill();

	}
}
